import { NgModule, Optional, SkipSelf, ModuleWithProviders } from '@angular/core';
import { throwIfAlreadyLoaded } from '../module-import-guard';

import { AuthConfig } from '../config/auth.config';
import { TokenGuard } from './token/token.guard';
import { TokenService } from './token/token.service';
import { CookieService } from './store/cookie.service';
import { STORE_TOKEN } from './auth.interface';

@NgModule({})
export class AuthModule {
  constructor(
    @Optional()
    @SkipSelf()
    parentModule: AuthModule,
  ) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }

  static forRoot(): ModuleWithProviders {
    return {
      ngModule: AuthModule,
      providers: [
        AuthConfig,
        TokenGuard,
        TokenService,
        {
          provide: STORE_TOKEN,
          useClass: CookieService
        }
      ]
    };
  }
}
